﻿using System.Windows.Forms;

namespace Domotica.Gazing
{
    internal partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();

            SetListBoxes();
        }

        private Config _Config;
        private string _CameraName;
        private int _CameraNumber;

        public void SetConfig(Config config)
        {
            _Config = config;
            
            SetValue(cbxSmoothing, _Config.Smoothing);
            SetValue(cbxHorizontalGain, _Config.HorizontalGain);
            SetValue(cbxVerticalGain, _Config.VerticalGain);
            cbReverseHorizon.Checked = _Config.ReverseHorizon;
            _CameraName = _Config.CameraName;
            _CameraNumber = _Config.CameraNumber;
        }

        public Config GetConfig()
        {
            _Config.Smoothing = GetValue(cbxSmoothing);
            _Config.HorizontalGain = GetValue(cbxHorizontalGain);
            _Config.VerticalGain = GetValue(cbxVerticalGain);
            _Config.ReverseHorizon = cbReverseHorizon.Checked;
            _Config.CameraNumber = _CameraNumber;
            _Config.CameraName = _CameraName;
            return _Config;
        }

        private void SetValue(ComboBox cbx, float value)
        {
            foreach (object c in cbx.Items)
            {
                ConfigValue cv = c as ConfigValue;
                if(cv != null)
                {
                    if (cv.Value <= value)
                    {
                        cbx.SelectedItem = cv;
                        return;
                    }
                }
            }
        }

        private float GetValue(ComboBox cbx)
        {
            ConfigValue cv = cbx.SelectedItem as ConfigValue;
            if (cv == null)
            {
                cv = cbx.Items[0] as ConfigValue;
            }
            if (cv == null)
            {
                return 0;
            }
            return cv.Value;
        }

        private void SetListBoxes()
        {
            cbxSmoothing.Items.AddRange(
                new[]
                    {
                        new ConfigValue("None", 1f),
                        new ConfigValue("Very Low", 0.95f),
                        new ConfigValue("Low", 0.8f),
                        new ConfigValue("Medium", 0.65f),
                        new ConfigValue("Medium High", 0.5f),
                        new ConfigValue("High", 3f),
                        new ConfigValue("Very High", 0.15f),
                        new ConfigValue("Extreme", 0.05f)
                    });
            cbxHorizontalGain.Items.AddRange(
                new[]
                    {
                        new ConfigValue("Very Low", 3f),
                        new ConfigValue("Low", 4.5f),
                        new ConfigValue("Medium", 6f),
                        new ConfigValue("Medium High", 7.5f),
                        new ConfigValue("High", 9f),
                        new ConfigValue("Very High", 10.5f),
                        new ConfigValue("Extreme", 12f)
                    });

            cbxVerticalGain.Items.AddRange(
                new[]
                    {
                        new ConfigValue("Very Low", 3f),
                        new ConfigValue("Low", 4.5f),
                        new ConfigValue("Medium", 6f),
                        new ConfigValue("Medium High", 7.5f),
                        new ConfigValue("High", 9f),
                        new ConfigValue("Very High", 10.5f),
                        new ConfigValue("Extreme", 12f)
                    });
        }

        private void btnChangeVideoSource_Click(object sender, System.EventArgs e)
        {
            SelectCameraForm f = new SelectCameraForm();
            f.SetValue(_CameraNumber, _CameraName);
            if (f.ShowDialog() == DialogResult.OK)
            {
                f.GetValue(ref _CameraNumber, ref _CameraName);
            }
        }

        private void btnChangeSettings_Click(object sender, System.EventArgs e)
        {
            using (Camera c = Camera.GetCamera(_CameraNumber))
            {
                c.ShowPropertiesDialog(this.Handle);
            }
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            GetConfig();
            _Config.Save();
            DialogResult = DialogResult.OK;
            Close();
        }
    }

    internal class ConfigValue
    {
        public ConfigValue(string title, float value)
        {
            Title = title;
            Value = value;
        }

        public string Title { get; set; }
        public float Value { get; set; }

        public override string ToString()
        {
            return Title;
        }
    }
}